package com.ruoyi.experiment.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.experiment.domain.Circulation;
import com.ruoyi.experiment.domain.Experiment;
import com.ruoyi.experiment.domain.Project;
import com.ruoyi.experiment.domain.Sample;
import com.ruoyi.experiment.mapper.ExperimentMapper;
import com.ruoyi.experiment.model.EProject;
import com.ruoyi.experiment.model.ExperimentFlume;
import com.ruoyi.experiment.service.IExperimentService;
import com.ruoyi.experiment.utils.Num;
import com.ruoyi.sample.mapper.SampleLzMapper;
import com.ruoyi.sample.mapper.SampleMapper;
import com.ruoyi.sample.model.SampleLz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ExperimentServiceImpl implements IExperimentService {

    @Autowired
    ExperimentMapper experimentMapper;
    @Autowired
    SampleLzMapper sampleLzMapper;
    @Autowired
    SampleMapper sampleMapper;

    @Override
    public List<Long> selectNoFlumeExperiments(Long id) {
        return experimentMapper.selectNoFlumeExperiments(id);
    }

    @Override
    public List<Long> selectNoFlumeProjects(Long id) {
        return experimentMapper.selectNoFlumeProjects(id);
    }

    @Override
    public List<EProject> selectAllItem(Project project) {
        return experimentMapper.selectAllItem(project);
    }

    @Override
    public Sample selectSampleById(Long id) {
        return experimentMapper.selectSampleById(id);
    }

    @Override
    public List<Sample> selectSamples(Sample sample) {
        return experimentMapper.selectSamples(sample);
    }

    @Override
    @Transactional
    public boolean callForSample(List<Sample> sample) {
        for (Sample s : sample) {
            if (s.getSampleStatus()!=null && s.getSampleStatus()>=1)
                continue;
            s.setSampleStatus(1);
            experimentMapper.insertMsg(6,s.getProjectId(), SecurityUtils.getUserId(),"请求流转样品:"+s.getSampleName());
            experimentMapper.updateSample(s);
        }
        return true;
    }

    @Override
    @Transactional
    public boolean flume(ExperimentFlume flume) {
        for (Long id : flume.getIds()) {
            Experiment experiment = experimentMapper.selectExperimentalManagementByExperimentId(id);
            //实验分发员
            if (flume.getPostId()==7){
                if (Num.get(experiment.getExpermentType(),2)>0){
                    continue;
                }
                experimentMapper.flumeExperiment(id,Num.set(experiment.getExpermentType(),2,1));
            } else if (flume.getPostId() == 8) {
                //项目负责人
                if (Num.get(experiment.getExpermentType(),3)>0){
                    continue;
                }
                experimentMapper.flumeExperiment(id,Num.set(experiment.getExpermentType(),3,1));
            }
        }
        List<Sample> samples = experimentMapper.selectSamplesByExperimentId(flume.getIds().toArray(new Long[0]));
        if (samples==null || samples.isEmpty())
            return false;
        for (Sample sample : samples) {
            Circulation circulation = new Circulation();
            circulation.setCirculationType(flume.getPostId());
            circulation.setProjectId(sample.getProjectId());
            circulation.setUserName(SecurityUtils.getUsername());
            circulation.setCirculationSuggestion(flume.getCirculationSuggestion());
            circulation.setInfo("quantity:"+ sample.getSampleQuantity()+";size:"+ sample.getSampleSize());
            experimentMapper.insertFlume(circulation);
            //样品流转至实验分发人
            if (flume.getPostId()==7) {
                confirmLZ(1<<3, sample.getSampleId());
                sample.setSampleStatus(3);
                sampleLzMapper.insertSampleLz(new SampleLz(sample.getSampleId(),"实验管理员流转至实验分发人", sample.getSampleQuantity(), sample.getSampleSize()));
                experimentMapper.updateSample(sample);
            }
        }

        return true;
    }
    private boolean confirmLZ(Integer status, Long id) {
        Sample sample = new Sample();
        sample.setSampleId(id);
        sample.setConfirmStatus(status);
        int i = sampleMapper.updateSample(sample);
        return i==1;
    }

    /**
     * 查询实验管理
     *
     * @param sampleId 实验管理主键
     * @return 实验管理
     */
    @Override
    public Experiment selectExperimentBySampleId(Long sampleId)
    {
        return experimentMapper.selectExperimentalBySampleId(sampleId);
    }

    /**
     * 查询实验管理列表
     *
     * @param experimentalManagement 实验管理
     * @return 实验管理
     */
    @Override
    public List<Experiment> selectExperimentalManagementList(Experiment experimentalManagement)
    {
        return experimentMapper.selectExperimentalManagementList(experimentalManagement);
    }

    /**
     * 新增实验管理
     *
     * @param experimentalManagement 实验管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertExperimentalManagement(Experiment experimentalManagement)
    {
        experimentMapper.updateProject(experimentalManagement.getSampleId());
        int i = experimentMapper.insertExperimentalManagement(experimentalManagement);
        Long pid = experimentMapper.selectProjectId(experimentalManagement.getSampleId());
        experimentMapper.updateAmount(pid);
        return i;
    }

    /**
     * 修改实验管理
     *
     * @param experimentalManagement 实验管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateExperimentalManagement(Experiment experimentalManagement)
    {
        int i = experimentMapper.updateExperimentalManagement(experimentalManagement);
        Long pid = experimentMapper.selectProjectId(experimentalManagement.getSampleId());
        experimentMapper.updateAmount(pid);
        return i;
    }

    /**
     * 删除实验管理
     *
     * @param sampleId 需要删除的实验管理主键
     * @return 结果
     */
    @Override
    public int deleteExperimentalManagementBySampleId(Long sampleId)
    {
        return experimentMapper.deleteExperimentalManagementBySampleId(sampleId);
    }
}
